using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Diagnostics;
using System.Web.Configuration;
using System.Xml;

namespace TempusFugate.TrendSetter.Services
{
  /// <summary>
  /// Summary description for incoming
  /// </summary>
  [WebService( Namespace = "http://tempusfugate.com/ws/" )]
  [WebServiceBinding( ConformsTo = WsiProfiles.BasicProfile1_1 )]
  public class Incoming : System.Web.Services.WebService
  {

    public Incoming()
    {

      //Uncomment the following line if using designed components 
      //InitializeComponent(); 
    } //Incoming

    [WebMethod]
    public bool PostMedia( int listenerid, string[][] attribs, out string response )
    {

      string connstr = "";
      string query = "pcins_incomingxml";
      bool success = false;
      SqlConnection connection;
      SqlCommand command;
      int rows = 0;
      XmlDocument prm = null;
      XmlElement element = null;
      XmlElement root = null;
      XmlText txt = null;

      try
      {
        ConnectionStringSettings connString;

        connString = ConfigurationManager.ConnectionStrings[ "post" ];
        if ( connstr != null )
        {
          connstr = connString.ConnectionString;
        }

        if ( connstr.Length < 2 )
        {
          throw new Exception( "No connection string found." );
        }

      }
      catch ( Exception cfg )
      {
        Trace.WriteLine( "Error Retrieving Configuration : " + cfg.ToString() );
        response = "Service temporarily offline.";
        return success;
      }

      try
      {

        connection = new SqlConnection( connstr );
        command = new SqlCommand( query, connection );
        command.CommandType = CommandType.StoredProcedure;

        command.Parameters.Add( new SqlParameter( "@listenerid", listenerid ) );
        rows = attribs[ 0 ].Length;
        prm = new XmlDocument();
        root = prm.CreateElement( "media" );
        prm.AppendChild( root );
        for ( int i = 0; i < rows; i++ )
        {
          element = prm.CreateElement( XmlConvert.EncodeLocalName(attribs[ 0 ][ i ].ToLower()) );
          txt = prm.CreateTextNode( attribs[ 1 ][ i ] );
          element.AppendChild( txt );
          root.AppendChild( element );
        } //for
        command.Parameters.Add( new SqlParameter( "@attributes", prm.InnerXml ) );
      }
      catch ( Exception qry )
      {
        Trace.WriteLine( "Error Setting Up Query: " + qry.ToString() );
        response = "Malformed attribute submission." ;
        return success;
      }

      try
      {

        using ( command.Connection )
        {
          command.Connection.Open();
          rows = command.ExecuteNonQuery();
          if ( rows > 0 ) { success = true; }

        } // using

        if ( connection.State != ConnectionState.Closed )
        {
          connection.Close();
        }

      }
      catch ( Exception exc )
      {
        Trace.WriteLine( "Error Executing Query: " + exc.ToString() );
        response = "Data services temporarily offline." ;
        return success;
      }

      response = "Success";
      return success;
    } //PostMedia

  } //Incoming

} //namespace TempusFugate.Trendsetter
